home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / RULER.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  14KB  |  507 lines

  1. /* Copyright (C) Stephen Chung, 1991-1993.  All rights reserved. */
  2.  
  3. #include "jwp.h"
  4.  
  5. #include "idm.h"
  6.  
  7.  
  8. static char StatusText[MAXLINELEN];
  9.  
  10.  
  11.  
  12. static int ClosestUnit (int x, int units)
  13. {
  14.     return ((x + (units / 2)) / units);
  15. }
  16.  
  17.  
  18.  
  19. void Triangles (FILEOPTIONS *f)
  20. {
  21.     RECT rect;
  22.  
  23.     if (!(f->type & FN_NORMAL) || f->hwnd == NULL || RULER(f).hwnd == NULL) return;
  24.  
  25.     if (RULER(f).startx == f->startx &&
  26.         RULER(f).left == CURPARA(f)->leftindent &&
  27.         RULER(f).right == CURPARA(f)->rightindent &&
  28.         RULER(f).first == CURPARA(f)->firstindent)
  29.         return;
  30.  
  31.     GetClientRect(RULER(f).hwnd, &rect);
  32.  
  33.     if (RULER(f).startx == f->startx) {
  34.         rect.top = RULER(f).upperrow + 1;
  35.         rect.bottom = RULERHEIGHT - 1;
  36.         rect.left = 0;
  37.  
  38.         InvalidateRect(RULER(f).hwnd, &rect, TRUE);
  39.     } else {
  40.         InvalidateRect(RULER(f).hwnd, NULL, TRUE);
  41.     }
  42.     UpdateWindow(RULER(f).hwnd);
  43. }
  44.  
  45.  
  46. static int DrawTriangle (HDC hdc, FILEOPTIONS *f, int pos, int which)
  47. {
  48.     POINT   pts[3];
  49.     BOOL    absolute = FALSE;
  50.  
  51.  
  52.     if (which < 0) {
  53.         which = -which;
  54.         absolute = TRUE;
  55.     }
  56.  
  57.  
  58.     switch (which) {
  59.  
  60.     case 1:
  61.         if (absolute) {
  62.             pts[1].x = pts[0].x = pos;
  63.         } else {
  64.             pts[1].x = pts[0].x = POS2PIXEL(f->linelen - pos);
  65.         }
  66.         pts[0].y = RULERHEIGHT - 2;
  67.         pts[1].y = RULER(f).upperrow + 1;
  68.         pts[2].y = RULER(f).middlerow;
  69.         pts[2].x = pts[0].x - RULER(f).pointer;
  70.         Polygon(hdc, pts, 3);
  71.         return (pts[0].x);
  72.  
  73.     case 2:
  74.         if (absolute) {
  75.             pts[1].x = pts[0].x = pos;
  76.         } else {
  77.             pts[0].x = pts[1].x = POS2PIXEL(pos);
  78.         }
  79.         pts[0].y = RULER(f).middlerow - 1;
  80.         pts[1].y = RULER(f).upperrow + 1;
  81.         pts[2].x = pts[0].x + RULER(f).pointer;
  82.         pts[2].y = RULER(f).middlerow - 1;
  83.         Polygon(hdc, pts, 3);
  84.         return (pts[0].x);
  85.  
  86.     case 3:
  87.         if (absolute) {
  88.             pts[1].x = pts[0].x = pos;
  89.         } else {
  90.             pts[0].x = pts[1].x = POS2PIXEL(pos);
  91.         }
  92.         pts[0].y = RULER(f).middlerow + 1;
  93.         pts[1].y = RULER(f).lowerrow - 1;
  94.         pts[2].x = pts[0].x + RULER(f).pointer;
  95.         pts[2].y = RULER(f).middlerow + 1;
  96.         Polygon(hdc, pts, 3);
  97.         return (pts[0].x);
  98.  
  99.     default:
  100.         return (0);
  101.     }
  102. }
  103.  
  104.  
  105.  
  106. LONG FAR PASCAL RulerProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  107. {
  108.     HDC         hdc;
  109.     PAINTSTRUCT ps;
  110.     RECT        rect;
  111.     FILEOPTIONS *f;
  112.     POSITION    p;
  113.     PARAGRAPH   far *pp;
  114.     HBRUSH      hbrush;
  115.     HPEN        hpen;
  116.     int         i, j, k, new;
  117.     static int  buttondown = 0;
  118.     static int  tripos;
  119.  
  120.  
  121.     for (f = fileoptions; f != NULL; f = f->next) if (f->ruler.hwnd == hwnd) break;
  122.  
  123.     if (f == NULL) f = curfile;
  124.  
  125.     
  126.     switch (message) {
  127.  
  128.     case WM_LBUTTONDOWN:
  129.         if (!buttondown) {
  130.             i = LOWORD(lParam);
  131.             j = HIWORD(lParam);
  132.  
  133.             /* Within the triangles? */
  134.  
  135.             k = POS2PIXEL(f->linelen - CURPARA(f)->rightindent) - i;
  136.  
  137.             if (k <= RULER(f).pointer && k >= 0 &&
  138.                 j >= RULER(f).upperrow && j <= RULER(f).lowerrow) {
  139.  
  140.                 buttondown = 1;         /* Right triangle */
  141.                 tripos = RULER(f).rightpos;
  142.                 goto Out1;
  143.             }
  144.  
  145.             k = POS2PIXEL(CURPARA(f)->firstindent) - i;
  146.  
  147.             if (k <= 0 && k >= -(RULER(f).pointer) &&
  148.                 j >= RULER(f).upperrow && j < RULER(f).middlerow) {
  149.  
  150.                 buttondown = 2;         /* Left upper triangle */
  151.                 tripos = RULER(f).firstpos;
  152.                 goto Out1;
  153.             }
  154.  
  155.             k = POS2PIXEL(CURPARA(f)->leftindent) - i;
  156.  
  157.             if (k <= 0 && k >= -(RULER(f).pointer) &&
  158.                 j > RULER(f).middlerow && j <= RULER(f).lowerrow) {
  159.  
  160.                 buttondown = 3;         /* Left lower triangle */
  161.                 tripos = RULER(f).leftpos;
  162.  
  163.                 if (RULER(f).left == RULER(f).first) buttondown = 4;
  164.  
  165.                 goto Out1;
  166.             }
  167.  
  168.             return (0);
  169.  
  170. Out1:       SetCapture(hwnd);
  171.         }
  172.         return (0);
  173.  
  174.     case WM_MOUSEMOVE:
  175.         if (!buttondown) return (0);
  176.  
  177.         i = LOWORD(lParam);
  178.         j = HIWORD(lParam);
  179.  
  180.         if (i < BORDERSPACE) i = BORDERSPACE;
  181.         if (i >= f->width) i = f->width - 1;
  182.  
  183.         new = i;
  184.  
  185.         if (buttondown == 1) {
  186.             if (RULER(f).first > RULER(f).left)
  187.                 k = RULER(f).firstpos + 2 * RULER(f).pointer;
  188.             else
  189.                 k = RULER(f).leftpos + 2 * RULER(f).pointer;
  190.  
  191.             if (new <= k) new = k + 1;
  192.         } else {
  193.             k = RULER(f).rightpos - 2 * RULER(f).pointer;
  194.             if (new >= k) new = k - 1;
  195.         }
  196.  
  197.         if (new != tripos) {
  198.             hdc = GetDC(hwnd);
  199.  
  200.             hbrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  201.             hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_WINDOW));
  202.  
  203.             SelectObject(hdc, hbrush);
  204.             SelectObject(hdc, hpen);
  205.             if (buttondown == 4) {
  206.                 DrawTriangle(hdc, f, tripos, -2);
  207.                 DrawTriangle(hdc, f, tripos, -3);
  208.             } else {
  209.                 DrawTriangle(hdc, f, tripos, -buttondown);
  210.             }
  211.  
  212.             SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  213.             SelectObject(hdc, GetStockObject(BLACK_PEN));
  214.             DeleteObject(hbrush);
  215.             DeleteObject(hpen);
  216.  
  217.             if (buttondown == 4) {
  218.                 DrawTriangle(hdc, f, new, -2);
  219.                 DrawTriangle(hdc, f, new, -3);
  220.             } else {
  221.                 DrawTriangle(hdc, f, new, -buttondown);
  222.             }
  223.             tripos = new;
  224.  
  225.             ReleaseDC(hwnd, hdc);
  226.         }
  227.  
  228.         return (0);
  229.  
  230.     case WM_LBUTTONUP:
  231.         if (!buttondown) return (0);
  232.  
  233.         hdc = GetDC(hwnd);
  234.  
  235.         hbrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  236.         hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_WINDOW));
  237.  
  238.         SelectObject(hdc, hbrush);
  239.         SelectObject(hdc, hpen);
  240.  
  241.         if (buttondown == 4) {
  242.             DrawTriangle(hdc, f, tripos, -2);
  243.             DrawTriangle(hdc, f, tripos, -3);
  244.         } else {
  245.             DrawTriangle(hdc, f, tripos, -buttondown);
  246.         }
  247.  
  248.         SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  249.         SelectObject(hdc, GetStockObject(BLACK_PEN));
  250.  
  251.         DeleteObject(hbrush);
  252.         DeleteObject(hpen);
  253.  
  254.         switch (buttondown) {
  255.             case 1:
  256.                 new = ClosestUnit(tripos - BORDERSPACE + f->startx,
  257.                                     f->basefont->width + f->leading);
  258.                 if (new <= RULER(f).first) new = RULER(f).first + 1;
  259.                 else if (new <= RULER(f).left) new = RULER(f).left + 1;
  260.                 new = f->linelen - new;
  261.  
  262.                 if (CURPARA(f)->rightindent != new) f->changed = TRUE;
  263.                 RULER(f).rightpos = DrawTriangle(hdc, f, new, 1);
  264.                 RULER(f).right = new;
  265.  
  266.                 if (SELPARA1(f) == NULL) {
  267.                     UndoAddFormatParagraph(f, f->current, f->current);
  268.                     CURPARA(f)->rightindent = new;
  269.                 } else {
  270.                     UndoAddFormatParagraph(f, SEL1(f), SEL2(f));
  271.                     for (pp = SELPARA1(f); pp != SELPARA2(f)->next; pp = pp->next)
  272.                         pp->rightindent = new;
  273.                 }
  274.                 break;
  275.             case 2:
  276.             case 4:
  277.                 new = ClosestUnit(tripos - BORDERSPACE + f->startx,
  278.                                         f->basefont->width + f->leading);
  279.                 if (new >= f->linelen - RULER(f).right)
  280.                     new = RULER(f).right - 1;
  281.  
  282.                 if (CURPARA(f)->firstindent != new) f->changed = TRUE;
  283.                 RULER(f).firstpos = DrawTriangle(hdc, f, new, 2);
  284.                 RULER(f).first = new;
  285.  
  286.                 if (SELPARA1(f) == NULL) {
  287.                     UndoAddFormatParagraph(f, f->current, f->current);
  288.                     CURPARA(f)->firstindent = new;
  289.                 } else {
  290.                     UndoAddFormatParagraph(f, SEL1(f), SEL2(f));
  291.                     for (pp = SELPARA1(f); pp != SELPARA2(f)->next; pp = pp->next)
  292.                         pp->firstindent = new;
  293.                 }
  294.                 if (buttondown == 2) break;
  295.  
  296.                 /* Falls through... */